package com.analytical.www.fargment

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.analytical.www.MainActivity
import com.analytical.www.R
import com.analytical.www.base.FragmentBase
import com.analytical.www.databinding.FragHomeBinding
import com.analytical.www.pop.PopInputSample
import com.analytical.www.pop.PopTestResult
import com.analytical.www.util.StringUtils.log
import com.analytical.www.util.StringUtils.toast
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *@Auther Shawn
 *创建时间：2023/12/24 12:34
 */
class FragHome:FragmentBase() {
    private var array1 = arrayOf("ABC","BCD","MXS")
    private var array2 = arrayOf("正十六烷","正二十烷","正五十烷")
    private var array3 = arrayOf("B1111","A222","S3333")
    private var select = 0
    private var index = 0
    private lateinit var binding:FragHomeBinding

    private val popTestResult by lazy {
        val value = PopTestResult(requireContext())
        value
    }
    override fun getLayoutId(): Int {
        return R.layout.frag_home
    }

    override fun lazyLoad() {
        binding = FragHomeBinding.bind(contentView)
        binding.includeHome1.iv1.isSelected = true
        initState()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun getData() {

    }

    private fun initState() {
        binding.includeHome2.tvServerState.text = "系统正常，请设置"
        binding.includeHome2.ivState.isSelected = false
    }

    fun isStartTest(): Boolean {
        return binding.includeHome2.ivState.isSelected
    }

    fun startTest() {
        lifecycleScope.launch {
            var num1 = 10
            var num2 = 10
            var num3 = 30
            repeat(8) {
                val temp1 = (8..15).random()
                num1 += temp1
                if (num1 < 95) {
                    binding.includeHome2.tv1Result.text = "${num1}℃"
                }
                val temp2 = (8..15).random()
                num2 += temp2
                if (num2 < 95) {
                    binding.includeHome2.tv2Result.text = "${num2}℃"
                }
                val temp3 = (10..20).random()
                num3 += temp3
                if (num3 < 110) {
                    binding.includeHome2.tv3Result.text = "${num3}kPa"
                }
                delay(1500L)
            }.runCatching {
                "测试结束".toast()
                initState()
                (context as MainActivity).changerBtnState(0)
            }
        }
    }

    override fun initClick() {
        binding.includeHome2.ivState.setOnClickListener {
            binding.includeHome2.ivState.isSelected = true
            binding.includeHome2.tvServerState.text = "系统正常,设置完毕，可测试"
            if (context is MainActivity) {
                (context as MainActivity).changerBtnState(1)
            }
        }
        binding.ivUp.setOnClickListener {
            if (index > array1.size - 1||index < 0) {
                index = 0
            }
            when(select) {
                0 -> {
                    binding.includeHome1.tv1.text = array1[index%array1.size]
                }
                1 -> {
                    binding.includeHome1.tv2.text = array2[index%array2.size]
                }
                2 -> {
                    binding.includeHome1.tv3.text = array3[index%array3.size]
                }
                else->{

                }
            }
            index++
        }
        binding.ivDown.setOnClickListener {
            if (index > array1.size - 1 || index < 0) {
                index = array1.size
            }
            when (select) {
                0 -> {
                    binding.includeHome1.tv1.text = array1[index % array1.size]
                }
                1 -> {
                    binding.includeHome1.tv2.text = array2[index % array2.size]
                }
                2 -> {
                    binding.includeHome1.tv3.text = array3[index % array3.size]
                }
                else -> {

                }
            }
            index--
        }
        binding.includeHome1.iv1.setOnClickListener {
            select = 0
            index = 0
            binding.includeHome1.iv1.isSelected = true
            binding.includeHome1.iv2.isSelected = false
            binding.includeHome1.iv3.isSelected = false
        }
        binding.includeHome1.iv2.setOnClickListener {
            select = 1
            index = 0
            binding.includeHome1.iv1.isSelected = false
            binding.includeHome1.iv2.isSelected = true
            binding.includeHome1.iv3.isSelected = false
        }
        binding.includeHome1.iv3.setOnClickListener {
            select = 2
            index = 0
            binding.includeHome1.iv1.isSelected = false
            binding.includeHome1.iv2.isSelected = false
            binding.includeHome1.iv3.isSelected = true
        }
    }

    override fun onKeyCodeBack(): Boolean {
        return false
    }

    companion object{
        fun newInstance(): FragHome {
            val args = Bundle()
            val fragment = FragHome()
            fragment.arguments = args
            return fragment
        }
    }
}